package com.tanhua.server.controller;

import com.tanhua.domain.db.PageResult;
import com.tanhua.domain.vo.FriendLinkedVo;
import com.tanhua.server.service.ContactsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/messages")
public class ContactsController {
    //注入ContactsService
    @Autowired
    private ContactsService contactsService;

    /**
     * 添加联系人
     * @param map
     * @return
     */
    @PostMapping("/contacts")
    public ResponseEntity addContacts(@RequestBody Map<String,Long> map) {
        //a.调用service 传入对方的id
        Long userId = map.get("userId");
        contactsService.addContacts(userId);
        //b.返回给前端状态码
        return ResponseEntity.ok(null);
    }

    /**
     * 查看好友列表
     */
    @GetMapping("/contacts")
    public ResponseEntity QueryContacts(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pagesize,
                                        @RequestParam(required = false) String keyword) {
        //三元运算处理请求参数为null的情况
        page = page > 0 ? page : 1;
        pagesize = pagesize > 0 ? pagesize : 10;
        //a.调用service层 传入page,pagesize,keywork参数 返回PageResult<FriendLinked>
        PageResult<FriendLinkedVo> linkedVoPageResult=
                contactsService.queryContacts(page,pagesize, keyword);
        //b.返回状态信息
        return ResponseEntity.ok(linkedVoPageResult);
    }



}
